package com.yth.utils.udf;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2024/12/16 14:06
 */
public class UdfLoader {
    private static final List<Class<? extends Udf>> UDF_CLASSES = new ArrayList<>();

    static {
        try {
            ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
            provider.addIncludeFilter(new AssignableTypeFilter(Udf.class));
            provider.setResourcePattern("**/*.class");
            Set<BeanDefinition> components = provider.findCandidateComponents("com");
            for (BeanDefinition component : components) {
                Class<?> cls = Class.forName(component.getBeanClassName());
                UDF_CLASSES.add((Class<? extends Udf>) cls);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static List<Class<? extends Udf>> getUdfClasses() {
        return Collections.unmodifiableList(UDF_CLASSES);
    }

    private UdfLoader() {
    }
}
